
*====================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                Processing Vignette data - Baseline
*====================================================================*

* Open data
  use "$rawdatap1\deidentified_vignettes_21clusters", clear
* Check number of clusters (80) and observations (396)
  duplicates report cluster
  assert r(unique_value)==80
  assert r(N)==396

* Sample Diarrhea and Pneumonia only
  drop c2* c4*
  d, short


***********************************************
* General Questions
***********************************************

* Source
  gen     source = "Vignette Baseline"
  lab var source "Data source"

* Wave
  gen     wave = 1
  lab var wave "Wave"

* Sample 
/*q11 status of interview: 1: completed; 2: partially completed; 4: facility was closed*/
  gen     sample4 = 1 
  replace sample4 = 3 if q5a==16102
  lab var sample4 "Sample"
  lab def sample4 1 "completed" 2 "partially completed" 3 "closed facility"
  lab val sample4 sample4
  tab sample4

* Providers ID
  gen     prov_id = q5a
  lab var prov_id "Provider ID"

* Location variables
  gen     district = q2 
  lab var district "District"
  gen     block = block_id 
  lab var block "Block"
  order cluster, after(block)
  lab var cluster "Cluster"
  gen     village = q3
  lab var village "Village"

* Interview related variables
  gen     int_code1 = q7a
  lab var int_code1 "Interviewer code"
  gen     int_code2 = q7b
  lab var int_code2 "Interviewer code"

* Survey date
  gen     survey_date=mdy(mm, dd, yyyy)
  format  survey_date %d
  lab var survey_date "Date of survey"

* Label yes/no
  label define yesno 0 "no" 1 "yes"

***********************************************
* Medical History Section - (c#h)
***********************************************

* Recoding variables
  foreach v of varlist c1h* c3h* {
  recode `v' (2=0)
  lab val `v' yesno
    }

* Recoding values according to extra questions
  replace c1h2=1 if prov_id==30401 | prov_id==16104
  replace c1h5=1 if prov_id==11303
  local h10 "27304 10305 24205 38203 6101 27905 23101 35604 35601 4702 605 24104 46204 32503 10502 41502 38903 2102 22502 15004 20501 35002"
  foreach v of local h10 {
  replace c1h10=1 if prov_id==`v'
  }
  replace c1h11=1 if prov_id==19403 | prov_id==20501
  replace c1h13=1 if prov_id==27103 | prov_id==16105
  replace c1h19=1 if prov_id==10305
  replace c1h26=1 if prov_id==27903
  replace c1h29=1 if prov_id==17503 | prov_id==13303

  replace c3h1=1 if prov_id==32502
  replace c3h5=1 if prov_id==32104 | prov_id==11304 | prov_id==10504 | prov_id==45501 | prov_id==5402
  replace c3h8=1 if prov_id==19403
  replace c3h21=1 if prov_id==33303
  replace c3h27=1 if prov_id==24105
  replace c3h37=1 if prov_id==19704


* Number of Medical History questions asked
  foreach n in 1 3 {
    if `n'==1 local m = 35
    if `n'==3 local m = 52
    egen    nhistory`n' = rowtotal(c`n'h1-c`n'h`m'), missing
    lab var nhistory`n' "No. of Medical History ques. asked"
    gen     phistory`n' = nhistory`n'/`m'
    lab var phistory`n' "Pct. of Medical History ques. asked"
    local total = `total' + `m'
    di in ye "`total'"
     }
  order phistory*, last

* CAUSE of disease: Essential questions
/*Diarrhea:
  c1h1  does the child have fever is the child warm
  c1h4  nature of stool
  c1h7  blood / mucus in the stools
  c1h10 does he complain of stomachache
  c1h14 vomiting
  c1h15 what are his feeding habits
  c1h23 do you boil drinking water

  Pneumonia:
  c3h1  is it a high fever
  c3h10 has he been breathing rapidly
  c3h12 does his nostrils appear to be flaring when he is breathing
  c3h13 have you noticed if skin between his ribs,or his/her tummy
  c3h14 have you noticed if his neck muscles are straining or standing out more */

  local ess1 "c1h1 c1h4 c1h7 c1h10 c1h14 c1h15 c1h23"
  local ness1: word count `ess1'

  local ess3 "c3h1 c3h10 c3h12 c3h13 c3h14"
  local ness3: word count `ess3'

  foreach n in 1 3 {
    egen    ncause`n' = rsum(`ess`n'')
    lab var ncause`n' "No. of ques. asked related to cause"
    gen     pcause`n' = ncause`n'/`ness`n''
    lab var pcause`n' "Pct. of ques. asked related to cause"
      }
  order pcause*, last

* SEVERITY of disease: Essential questions
/*Diarrhea: 
  c1h2 is the urine normal /urine color
  c1h5 frequency of stool
  c1h6 quantity of stool
  c1h13 is the child weak now
  c1h19 is the child drinking a lot of water how manyglasses of water does he drink

  Pneumonia:
  c3h19 did you notice if his fingernails,hands or toes changed colour
  c3h35 is the child weak now
  c3h37 is there any change in frequency of urine
  c3h38 does he have tears when he cries  */

  local sev1 "c1h2 c1h5 c1h6 c1h13 c1h19"
  local nsev1: word count `sev1'

  local sev3 "c3h19 c3h35 c3h37 c3h38"
  local nsev3: word count `sev3'

  foreach n in 1 3 {
    egen    nseverity`n' = rsum(`sev`n'')
    lab var nseverity`n' "No. of ques. asked related to severity"
    gen     pseverity`n' = nseverity`n'/`nsev`n''
    lab var pseverity`n' "Pct. of ques. asked related to severity"
      }
  order pseverity*, last

* Essential items obtained by providers in history-taking
  foreach n in 1 3 {
    egen    nessential`n' = rsum(ncause`n' nseverity`n')
    lab var nessential`n' "No. of essential items obtained in history-taking"
      }

***********************************************
* EXAMINATION SECTION - (c#e)
***********************************************
/*Diarrhea:
  c1e1 length of child
  c1e2 weight of child
  c1e3 respiration rate
  c1e4 auscultation of chest and heart
  c1e5 pulse rate
  c1e6 temperature
  c1e7 mucous membranes for moistness
  c1e8 skin color and turgor
  c1e9 palpation of the abdomen
  c1e10 reflexes
  c1e11 mouth examination
  c1e12 child has long nails

  Pneumonia
  c3e1 lenth of child
  c3e2 weight of child
  c3e3 respiration rate
  c3e4 auscultation of chest and heart
  c3e5 pulse rate
  c3e6 temperature
  c3e7 mucous membranes for moistness
  c3e8 skin color and turgor
  c3e9 examination of chest
  c3e10 palpation of the abdomen
  c3e11 reflexes
  c3e12 mouth examination
  c3e13 throat examination */

* Recoding variables
  foreach v of varlist c1e* c3e* {
  recode `v' (2=0)
  lab val `v' yesno
    }

* Relevant Examination questions
  local exam1 "c1e2 c1e6 c1e7 c1e8 c1e9"
  local nexam1: word count `exam1'

  local exam3 "c3e2 c3e3 c3e4 c3e5 c3e6 c3e9"
  local nexam3: word count `exam3'

  foreach n in 1 3 {
    egen    nexam`n' = rsum(`exam`n'')
    lab var nexam`n' "No. of relevant examination taken"
    }


***********************************************
* Investigation Section - (c#i)
***********************************************
/*Diarrhoea
  c1i1 blood for serum electrolytes
  c1i2 tlc
  c1i3a neutrophils
  c1i3b eosinophils
  c1i3c lymphoyctes
  c1i3d monoytes
  c1i4 blood for hb.
  c1i5 stool test

  Pneumonia:
  c3i1 chest x-ray
  c3i2 hb
  c3i3 mean corpuscular volume
  c3i4 tlc
  c3i5.a neutrophils
  c3i5.b basophils
  c3i5.c eosinophils
  c3i5.d lymphoyctes
  c3i5.e monocytes
  c3i5.f platelet count*/

* Recoding variables
  foreach v of varlist c1i1-c1i6 c3i1-c3ii5 {
    recode `v' (2=0)
    lab val `v' yesno
      }

* Recoding values according to extra questions
  replace c1i5=1 if prov_id==25001

* No. of investigation questions asked
  egen    ninvest1=rsum(c1i1-c1i5)
  lab var ninvest1 "No. of investigation questions asked"

  egen    ninvest3=rsum(c3i1-c3i5_f)
  lab var ninvest3 "No. of investigation questions asked"

* Other question/examinations done
  gen     other1 =(c1i6==1)
  lab var other1 "Other question/examination done"
  gen     other3 =(c3ii5==1)
  lab var other3 "Other question/examination done"


***********************************************
* Diagnosis - (c#d)
***********************************************

* Gave any diagnosis
/*cxd1 on the basis of tests and examinations, would you be able to 
  say what this patient is suffering from?
  cxd1a code
    1 allergy
    2 anemia
    3 ashtma
    4 cancer
    5 cough
    6 cough and fever
    7 dehydration
    8 diarrhea
    9 dysentery
   10 fever
   11 gas
   12 infection
   13 influenza
   14 jaundice
   15 kala azar
   16 loose motion (simple)
   17 malaria
   18 pneumonia
   19 stomach problem
   20 tuberculosis (tb)
   21 typhoid
  888 could not diagnose
  999 others */

* Gave any diagnosis
  foreach n in 1 3 {
    gen     dx_any`n' = 1
    replace dx_any`n' = 0 if c`n'd1a==888
    lab var dx_any`n' "=1 if any kind of diagnosis was given"
      }

* Type of Diagnosis
  foreach n in 1 3 {
    gen     dx`n' = c`n'd1
    lab var dx`n' "Diagnosis"
    gen     dx_code`n' = c`n'd1a
    lab var dx_code`n' "Diagnosis code"
    lab val dx_code`n' c`n'd1a
    }
  order dx_code*, last

* Correct diagnosis
  lab def type 1 "correct" 0 "incorrect"

  * Diarrhea
    gen code_c1=soundex(c1d1)
    gen     dx_type1 = 0 
    replace dx_type1 = 1 if dx_code1==8  &  code_c1!="B236"                                       //no bacterial diarrhea
    replace dx_type1 = 1 if dx_code1==16 & (code_c1=="L253" | code_c1=="L223" | code_c1=="L353")  //loose motion
    lab var dx_type1 "=1 if diagnosis was correct"
    lab val dx_type1 type

  * Pneumonia
    gen code_c3=soundex(c3d1)
    gen     dx_type3 = 0
    replace dx_type3 = 1 if dx_code3==18 & (c3d1!="PNEUMONIA TYPHAID" & c3d1!="PNEUMONIA TYPHOID" & c3d1!="PNEUMONIA/THYHIOD HO SAKTI HAI" & code_c3!="N553" & code_c3!="T135")
    lab var dx_type3 "=1 if diagnosis was correct"
    drop code_c*


***********************************************
* Treatment
***********************************************

* Gave Any Medication/Treatment
/*c1t3: Oral rehydratation soluction by Electral in water
  c1t6: Oral rehydratation solution by salt, sugar and lemon in water */
  foreach n in 1 3 {
    egen    tx_any`n'=rownonmiss(c`n't1*5)
    recode  tx_any`n' (2/max=1)
    lab var tx_any`n' "=1 if any treatment was given"
    lab val tx_any`n' yesno
    }
  replace tx_any1 = 1 if c1t3==1 | c1t6==1


***********************************************
* Correct Treatment
***********************************************

* Merge
  sort prov_id
  merge 1:1 prov_id using "$rawdatap1\correct_treatment_vig"
  drop _merge

* Order
  foreach l in a b c d {
  order c1t1`l'1, before(c1t1`l'2)
  order c1t1`l'5, after(c1t1`l'4)
  order c3t1`l'1, before(c3t1`l'2)
  order c3t1`l'5, after(c3t1`l'4)
  }

* Rename
  rename tx_diarrhea tx_detail1
  rename tx_pneumonia tx_detail3
  lab var tx_detail1 "detailed treatment"
  lab var tx_detail3 "detailed treatment"
  lab var nmedicines1 "# of medicines prescribed"
  lab var nmedicines3 "# of medicines prescribed"
  lab var nantibiotics1 "# of antibiotics"
  lab var nantibiotics3 "# of antibiotics"

* Medicine code
  #delimit ;
  lab def medcode 
  1 "Unlabelled Tablets"
  2 "Unlabelled injections"
  3 "IV bottles/glucose drip"
  4 "Ayurvedic Medicines"
  5 "Homeopathic Medicines"
  6 "Antibiotics"
  7 "Analgesics"
  8 "Anti-ulcer medication"
  9 "Steroids (NSAIDS)" 
  10 "Anti-allergy medicines"
  11 "Cardiac medication"
  12 "Psychiatric/neural medicines"
  13 "Identified as another type of medication"
  14 "Household remedies"
  50 "Cannot determine medicine type";
  #delimit cr

* Medicines 
  foreach n in 1 3 {
    gen     medicines1`n'=c`n't1a5
    lab var medicines1`n' "Medicine 1"
    lab val medicines1`n' medcode
    gen     medicines2`n'=c`n't1b5
    lab var medicines2`n' "Medicine 2"
    lab val medicines2`n' medcode
    gen     medicines3`n'=c`n't1c5
    lab var medicines3`n' "Medicine 3"
    lab val medicines3`n' medcode
    gen     medicines4`n'=c`n't1d5
    lab var medicines4`n' "Medicine 4"
    lab val medicines4`n' medcode
    }

***********************************************
* IRT Diarrhea
***********************************************

/*Selected items
  1  c1h1 does the child have fever is the child warm
  2  c1h2 is the urine normal /urine color
  3  c1h3 when did the child last urinate
  4  c1h4 nature of stool
  5  c1h5 frequency of stool
  6  c1h6 quantity of stool
  7  c1h7 blood / mucus in the stools
  8  c1h8 worms in stools
  9  c1h9 foul smelling
  10 c1h10 does he complain of stomachache
  11 c1h11 does he cry while passing stools
  12 c1h13 is the child weak now
  13 c1h14 vomiting
  14 c1h19 is the child drinking a lot of water how manyglasses of water does he drin
  15 c1h30 is the child teething
  16 c1e2 weight of child
  17 c1e6 temperature
  18 c1e7 mucous membranes for moistness
  19 c1e8 skin color and turgor
  20 c1e9 palpation of the abdomen */

* Selected items should begin with the same string
  local i 1
  foreach v of varlist c1h1-c1h11 c1h13 c1h14 c1h19 c1h30 c1e2 c1e6-c1e9 {
    local varlab`i' : variable label `v'
    gen vig_item_d`i'1=`v'
    lab var vig_item_d`i'1 "`varlab`i''"
    local i= `i' +1
      }

* Estimate theta
  preserve
  keep if sample==1
  keep prov_id vig_item_d*
  openirt, id(prov_id) item_prefix(vig_item_d) save_item_parameters("$irtdata1\vig_items_parameters_1.dta") save_trait_parameters("$irtdata1\vig_traits_1.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata1\vig_traits_1.dta"
  renpfix theta vig_theta
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_std1 = std(vig_theta_eap)
  lab var vig_theta_eap_std1 "Standardized Theta (EAP)"
  egen    vig_theta_mle_std1 = std(vig_theta_mle)
  lab var vig_theta_mle_std1 "Standardized Theta (MLE)"
  drop vig_theta_pv1-vig_theta_pv5 vig_theta_mle_se

  rename vig_theta_eap vig_theta_eap1
  rename vig_theta_mle vig_theta_mle1

***********************************************
* IRT Pneumonia
***********************************************

/*Selected items
  1 c3h1 is it a high fever
  2 c3h2 is the cough continous
               3 c3h3 does the cough increase at night (not in the follow up)
  4 c3h4 did both fever and cough start five days ago
  5 c3h5 does he have a runny nose/is his nose blocked
               6 c3h6 does he have any vomiting        (not in the follow up)
  7 c3h10 has he been breathing rapidly
  8 c3h12 does his nostrils appear to be flaring when he is breathing
  9 c3h13 have you noticed if skin between his ribs,or his/her tummy move inwards wh
  10 c3h18 any particular sounds that he made since he started having difficulty
  11 c3h26 has he had breathlessness in the past
  12 c3e2 weight of child
  13 c3e3 respiration rate
  14 c3e4 auscultation of chest and heart
  15 c3e5 pulse rate
  16 c3e6 temperature
  17 c3e9 examination of chest
  18 c3i1 chest x-ray
  19 c3i4 tlc
  20 c3i5.a neutrophils
  21 c3i5.b basophils
  22 c3i5.c eosinophils
  23 c3i5.d lymphoyctes
  24 c3i5.e monocytes */


* Selected items should begin with the same string
  local i 1
  foreach v of varlist c3h1 c3h2 c3h4 c3h5 c3h10 c3h12 c3h13 c3h18 c3h26 c3e2-c3e6 c3e9 c3i1 c3i4 {
  local varlab`i' : variable label `v'
  gen vig_item_p`i'3=`v'
  lab var vig_item_p`i'3 "`varlab`i''"
  local i= `i' +1
    }

  egen    vig_item_p183=anymatch(c3i5_a c3i5_b c3i5_c c3i5_d c3i5_e), v(1)
  lab var vig_item_p183 "WBC differential - neutrophils,basophils,eosinophils,lymphoyctes,monocytes"


* Estimate theta
  preserve
  keep if sample==1
  keep prov_id vig_item_p*
  openirt, id(prov_id) item_prefix(vig_item_p) save_item_parameters("$irtdata1\vig_items_parameters_3.dta") save_trait_parameters("$irtdata1\vig_traits_3.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata1\vig_traits_3.dta"
  renpfix theta vig_theta
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_std3 = std(vig_theta_eap)
  lab var vig_theta_eap_std3 "Standardized Theta (EAP)"
  egen    vig_theta_mle_std3 = std(vig_theta_mle)
  lab var vig_theta_mle_std3 "Standardized Theta (MLE)"
  drop vig_theta_pv1-vig_theta_pv5 vig_theta_mle_se

  rename vig_theta_eap vig_theta_eap3
  rename vig_theta_mle vig_theta_mle3


*====================================================================*
* IRT CALCULATIONS BASED ON 12 QUESTIONS THAT MATCHED 
* BETWEEN VIGNETTES AND SSP 
*====================================================================*

* Diarrhea
  egen vig_dq11 = anymatch(c1h4 c1h7 c1h8 c1h9), v(1)
  gen  vig_dq21 = c1h5
  gen  vig_dq31 = c1h6
  egen vig_dq41 = anymatch(c1h2 c1h3), v(1)
  gen  vig_dq51 = c1h13
  gen  vig_dq61 = c1h1
  gen  vig_dq71 = c1h10
  gen  vig_dq81 = c1h14
  egen vig_dq91 = anymatch(c1h22 c1h23), v(1)
  egen vig_dq101 = anymatch(c1h15 c1h17), v(1)
  gen  vig_dq111 = c1h19
  egen vig_dq121 = anymatch(c1h24 c1h25 c1h26), v(1)

  lab var vig_dq11 "Diarrhea: Qualities of stool"
  lab var vig_dq21 "Diarrhea: Frequency"
  lab var vig_dq31 "Diarrhea: Quantity of stool"
  lab var vig_dq41 "Diarrhea: Questions about urination"
  lab var vig_dq51 "Diarrhea: Is the child active/playful?"
  lab var vig_dq61 "Diarrhea: Fever"
  lab var vig_dq71 "Diarrhea: Abdominal pain"
  lab var vig_dq81 "Diarrhea: Vomiting"
  lab var vig_dq91 "Diarrhea: Source of drinking water"
  lab var vig_dq101 "Diarrhea: What has the child eaten?"
  lab var vig_dq111 "Diarrhea: Taking fluids"
  lab var vig_dq121 "Diarrhea: Physical Environment"

* Estimate theta
  preserve
  keep if sample==1
  keep prov_id vig_dq*
  openirt, id(prov_id) item_prefix(vig_dq) save_item_parameters("$irtdata1\vig_items_parameters_1_q12.dta") save_trait_parameters("$irtdata1\vig_traits_1_q12.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata1\vig_traits_1_q12.dta", keepus(theta_eap theta_mle) 
  rename theta_eap vig_theta_eap_q121
  rename theta_mle vig_theta_mle_q121
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_q12_std1 = std(vig_theta_eap_q121)
  lab var vig_theta_eap_q12_std1 "Standardized Theta (EAP)"
  egen    vig_theta_mle_q12_std1 = std(vig_theta_mle_q121)
  lab var vig_theta_mle_q12_std1 "Standardized Theta (MLE)"


* PNEUMONIA
  egen vig_pq13 = anymatch(c3h1 c3h4), v(1)
  gen  vig_pq23 = c3h10
  egen vig_pq33 = anymatch(c3h12 c3h13 c3h14 c3h17), v(1)
  egen vig_pq43 = anymatch(c3h2 c3h3), v(1)
  gen  vig_pq53 = c3h5
  gen  vig_pq63 = c3h18
  gen  vig_pq73 = c3h19
  gen  vig_pq83 = c3h35
  egen vig_pq93 = anymatch(c3h26 c3h40 c3h41), v(1)
  egen vig_pq103 = anymatch(c3h25 c3h27), v(1)
  gen  vig_pq113 = c3h49
  gen  vig_pq123 = c3h22

  lab var vig_pq13 "Pneumonia: Fever"
  lab var vig_pq23 "Pneumonia: Breathing is rapid"
  lab var vig_pq33 "Pneumonia: Difficulty in breathing/nostrils flaring/skin between ribs moves inwards/neck muscles are strained"
  lab var vig_pq43 "Pneumonia: Type of cough"
  lab var vig_pq53 "Pneumonia: Runny nose or nose blocked"
  lab var vig_pq63 "Pneumonia: Sounds while breathing"
  lab var vig_pq73 "Pneumonia: Change in colour of fingernails, hand, toes"
  lab var vig_pq83 "Pneumonia: Child is weak"
  lab var vig_pq93 "Pneumonia: Breastfeeding/immunization history"
  lab var vig_pq103 "Pneumonia: History of asthma/alleries"
  lab var vig_pq113 "Pneumonia: Have a chula at home"
  lab var vig_pq123 "Pneumonia: Other children have similar fever/cough"

* Estimate theta
  preserve
  keep if sample==1
  keep prov_id vig_pq*
  openirt, id(prov_id) item_prefix(vig_pq) save_item_parameters("$irtdata1\vig_items_parameters_3_q12.dta") save_trait_parameters("$irtdata1\vig_traits_3_q12.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata1\vig_traits_3_q12.dta", keepus(theta_eap theta_mle) 
  rename theta_eap vig_theta_eap_q123
  rename theta_mle vig_theta_mle_q123
  drop _merge
  drop id

* Standardizing thetas
  egen    vig_theta_eap_q12_std3 = std(vig_theta_eap_q123)
  lab var vig_theta_eap_q12_std3 "Standardized Theta (EAP)"
  egen    vig_theta_mle_q12_std3 = std(vig_theta_mle_q123)
  lab var vig_theta_mle_q12_std3 "Standardized Theta (MLE)"


***********************************************
* Recomendations
***********************************************

* Referral
  gen rec_referral1 = (c1t12==1)
  gen rec_referral3 = (c3t2==1)
  foreach n in 1 3 {
    lab var rec_referral`n' "Recommendation for referral"
    lab val rec_referral`n' yesno
    }

* Changes in diet,hygiene,changes in living conditions
  gen rec_hygiene_diet1 = (c1t10==1|c1t11==1)
  gen rec_hygiene_diet3 = (c3t5==1|c3t6==1)
  foreach n in 1 3 {
    lab var rec_hygiene_diet`n' "Recs. for changes in diet, hygiene and living conditions"
    lab val rec_hygiene_diet`n' yesno
    }

* Hospitalization
  gen     hospitalization3=(c3t3==1)
  lab var hospitalization "Hospitalizacion recommended for Pneumonia"

* Return
  gen     rec_return1 = (c1t9==1)
  lab var rec_return1 "Recs. on when to return or how to recognize a worsening situation"
  gen     rec_return3 = (c3t4==1)
  lab var rec_return3 "Recs. on when to return or how to recognize a worsening situation"

* ORS
  egen    rec_ors1 =anymatch(c1t3 c1t6), v(1)
  replace rec_ors1 = 2 if c1t4==1 | c1t7==1
  replace rec_ors1 = 3 if c1t5==1 | c1t8==1
  lab var rec_ors1 "Recs. related to ORS - c1t3 and c1t6" 
  lab def rec_ors 0 "ORS not recommended" 1 "ORS recommended" 2 "ORS recommended + preparation" 3 "ORS recommended + boiling water"
  lab val rec_ors1 rec_ors

***********************************************
* Particular Treatment
***********************************************
* Different Treatment because poor
  gen poor_tx1 = c1pt7
  gen poor_tx3 = c3pt5
  foreach n in 1 3 {
    lab var poor_tx`n' "What would you do if the patient were poor?"
    lab val poor_tx`n' c1pt7
    }

* Medicines because of poor
  foreach n in 1 3 {
  if `n'==1 local l "8"
  if `n'==3 local l "6"
    gen     med_ifpoor1`n'=c`n'pt`l'a5
    lab var med_ifpoor1`n' "Medicine 1 if poor"
    lab val med_ifpoor1`n' medcode
    gen     med_ifpoor2`n'=c`n'pt`l'b5
    lab var med_ifpoor2`n' "Medicine 2 if poor"
    lab val med_ifpoor2`n' medcode
    gen     med_ifpoor3`n'=c`n'pt`l'c5
    lab var med_ifpoor3`n' "Medicine 3 if poor"
    lab val med_ifpoor3`n' medcode
    gen     med_ifpoor4`n'=c`n'pt`l'd5
    lab var med_ifpoor4`n' "Medicine 4 if poor"
    lab val med_ifpoor4`n' medcode
    }

* Test recommended if poor
  gen     poor_test1 = c1pt5
  lab var poor_test1 "if this patient was very poor,what tests would you recommend"
  lab val poor_test1 c3pt3
  gen     poor_test3 = c3pt3
  lab var poor_test3 "if this patient was very poor,what tests would you recommend"
  lab val poor_test3 c3pt3

* Test if poor
  gen     test_ifpoor11=c1pt61
  lab var test_ifpoor11 "Test if poor"
  gen     test_ifpoor13=c3pt4a
  lab var test_ifpoor13 "Test if poor"

* Fee
  gen fee1 = c1pt9 if c1pt9!=999
  gen fee3 = c3pt7 if c3pt7!=999
  foreach n in 1 3 {
    lab var fee`n' "how much first time would you charge for this patient (including medicines)"
    }


***********************************************
* Reshape data
* Now we'll have two rows per provider. one for
* diarrhea and the other for pneumonia
***********************************************
* Keep relevant variables
  gen last =. 
  keep source-last
  drop last

* Define varlist to reshape
  #delimit ;
  local varlist "nhistory phistory ncause pcause nseverity pseverity nessential
  nexam ninvest other dx_any dx dx_code dx_type tx_any nantibiotics antib_dontknow
  nmedicines medicines1 medicines2 medicines3 medicines4 tx_type tx_detail
        rec_referral rec_hygiene_diet hospitalization rec_return rec_ors poor_tx
  med_ifpoor1 med_ifpoor2 med_ifpoor3 med_ifpoor4 poor_test test_ifpoor1 fee
  vig_dq1 vig_dq2 vig_dq3 vig_dq4 vig_dq5 vig_dq6 vig_dq7 vig_dq8 vig_dq9 vig_dq10 vig_dq11 vig_dq12
  vig_pq1 vig_pq2 vig_pq3 vig_pq4 vig_pq5 vig_pq6 vig_pq7 vig_pq8 vig_pq9 vig_pq10 vig_pq11 vig_pq12
  vig_theta_eap vig_theta_mle vig_theta_eap_std vig_theta_mle_std
  vig_theta_eap_q12 vig_theta_mle_q12 vig_theta_eap_q12_std vig_theta_mle_q12_std
  vig_item_d1 vig_item_d2 vig_item_d3 vig_item_d4 vig_item_d5 vig_item_d6 vig_item_d7 vig_item_d8 vig_item_d9 vig_item_d10
  vig_item_d11 vig_item_d12 vig_item_d13 vig_item_d14 vig_item_d15 vig_item_d16 vig_item_d17 vig_item_d18 vig_item_d19 vig_item_d20
  vig_item_p1 vig_item_p2 vig_item_p3 vig_item_p4 vig_item_p5 vig_item_p6 vig_item_p7 vig_item_p8 vig_item_p9 vig_item_p10
  vig_item_p11 vig_item_p12 vig_item_p13 vig_item_p14 vig_item_p15 vig_item_p16 vig_item_p17 vig_item_p18"
  ;
  #delimit cr

* Mantain variables labels
  foreach v of local varlist { 
    cap local l`v' : variable label `v'1
    if _rc!=0 {
    cap local l`v': variable label `v'2
    if _rc!=0 {
    local l`v': variable label `v'3
    }
      }
    }

* Reshape
   reshape long `varlist', i(prov_id) j(case)

* Labels
  foreach v of local varlist { 
   label var `v' `"`l`v''"' 
    }

  lab var vig_item_p1 "c3h1 is it a high fever"

* Identify disease
  recode case (3=2)
  lab var case "Type of disease"
  lab def case 1 "Diarrhea" 2 "Pneumonia", replace
  lab val case case

***********************************************
* Saving processed data DIARRHEA
***********************************************
  preserve
  keep if case==1

  drop  vig_pq1-vig_pq12 vig_item_p1-vig_item_p18

  forvalues n=1/20 {
    rename vig_item_d`n' vig_item`n'
    }

  #delimit ;
  lab def txd 1 "ORS +/- zinc" 
              2 "ORS + ayur/homeo/IV bottles/glucose drip"  
        3 "ORS + Antibiotics"  
        4 "ORS + Antibiotics + Others" 
        5 "ORS + Others (no antib)" 
        6 "No ORS, no harmful" 
        7 "No ORS and harmful (antib or others)" 
        8 "No treatment", replace;
  #delimit cr
  lab val tx_detail txd
  lab var tx_detail "Type of treatment for Diarrhea"
  
  label data "Vignette Diarrhea Baseline"
  compress
  save "$prodata1\vignette_diarrhea", replace
  restore


***********************************************
* Saving processed data PNEUMONIA
***********************************************

  preserve
  keep if case==2
  
  drop vig_dq1-vig_dq12 vig_item_d1-vig_item_d20
  
  forvalues n=1/18 {
    rename vig_item_p`n' vig_item`n'
    }
  
  #delimit ;
  lab def txp 
  1 "Antibiotics +/- no harm" 
  2 "Antibiotics + analgesics +/- no harm"
  3 "Antibiotics + analgesics + unnecesary +/- no harm"
  4 "Antibiotics + unnecesary +/- no harm"
  5 "No Antibiotics"
  6 "No treatment", replace;
  #delimit cr
  lab val tx_detail txp

  label data "Vignette Pneumonia Baseline"
  compress
  save "$prodata1\vignette_pneumonia", replace
  restore
